<?php
// +----------------------------------------------------------------------
// | Diygw PHP
// +----------------------------------------------------------------------
// | Copyright (c) 2022~2024 https://www.diygw.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: diygw <diygwcom@diygw.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\shop\controller;

use app\BaseController;
use app\shop\model\OrderModel;
use app\diy\model\UserModel;
use app\shop\model\SettingModel;
use EasyWeChat\Pay\Application;
use EasyWeChat\Pay\Message;
use think\App;
use think\facade\Db;

/**
 * @package app\shop\controller
 */
class IndexController extends BaseController
{
    //是否显示所有数据
    public $isAll = true;
    //是否初始化模型
    public $isModel = false;
    //判断是否全部不需要登录
    public $notNeedLoginAll = false;
    //判断不需要登录的方法
    public $notNeedLogin = ['index'];

    public function index(){
        $sql = 'select count(*) goodsTotal from shop_goods';
        $goodsTotal = Db::query($sql)[0];

        $sql = 'select count(*) userTotal from diy_user';
        $userTotal = Db::query($sql)[0];

        $sql = 'select sum(total) currentPayTotal,count(*) currentOrderTotal,COUNT(DISTINCT user_id) AS currentUserTotal  from shop_order WHERE DATE(create_time) = CURDATE() and pay_status=1';
        $currentPayTotal = Db::query($sql)[0];

        $sql = 'select count(*) currentUserTotal  from shop_order WHERE DATE(create_time) = CURDATE()';
        $currentUserTotal = Db::query($sql)[0];

        $sql = 'select sum(total) payTotal,COUNT(DISTINCT user_id) AS userPayTotal from shop_order';
        $payTotal = Db::query($sql)[0];

        $sql = 'select count(*) currentAddTotal  from diy_user WHERE DATE(create_time) = CURDATE()';
        $currentAddTotal = Db::query($sql)[0];

        $sql = 'select sum(total) total,status from shop_order GROUP BY status';
        $payStatusTotals = Db::query($sql);

        $payStatusTotal = [];
        $total = 0;
        foreach ($payStatusTotals as $item){
            $total= $total + $item['total'];
            $payStatusTotal['status'.$item['status'].'Total'] = $item['total'];
        }
        $payStatusTotal['statusTotal'] = $total;
        $data = array_merge($goodsTotal,$userTotal,$currentPayTotal,$currentUserTotal,$payTotal,$currentAddTotal,$payStatusTotal);

        $sql="select create_time date, sum( total ) total  from 	( select date_format( create_time, '%Y-%m-%d' ) as create_time, total from shop_order where pay_status=1 and create_time >= curdate( ) - interval 7 day ) t  group by 	create_time";
        $dateData = Db::query($sql);

        // 创建一个DateTime对象，表示当前时间

        $currentDate = new \DateTime();
        // 初始化一个空数组来存储日期
        $dates = [];
        // 循环七次，获取最近一周的日期
        for ($i = 6; $i >=0; $i--) {
            // 克隆当前DateTime对象，因为DateTime对象是不可变的
            $date = clone $currentDate;
            // 修改日期，减去相应的天数
            $date->modify("-$i day");
            // 将日期格式化为字符串，并添加到数组中
            // 你可以根据需要格式化日期，例如 'Y-m-d' 表示年-月-日
            $dates[] = $date->format('Y-m-d');
        }

        $dateTotals = [];
        foreach ($dateData as $dateData){
            $dateTotals[$dateData['date']] = $dateData['total'];
        }

        $series = [];
        foreach ($dates as $date){
            if(isset($dateTotals[$date])){
                $series[]=$dateTotals[$date];
            }else{
                $series[]=0;
            }

        }
        $data['dates'] =$dates;
        $data['series'] =$series;
        return $this->successData($data);


    }
}
